package com.example.srm.notice.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.srm.auth.anno.Authority;
import com.example.srm.common.controller.BaseController;
import com.example.srm.constant.NoticeConstant;
import com.example.srm.constant.ProjectStatusConstant;
import com.example.srm.domain.TNotice;
import com.example.srm.domain.TUser;
import com.example.srm.domain.vo.PageView;
import com.example.srm.domain.vo.Result;
import com.example.srm.notice.mapper.TNoticeMapper;
import com.example.srm.notice.service.TNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author pfk
 * @creatTime 2021/12/02 13:09
 * @describe    通知操作
 */
@RestController
@RequestMapping("/notice")
@Slf4j
@CrossOrigin
public class NoticeController extends BaseController<TNoticeService, TNotice> {

    @Autowired
    private TNoticeMapper noticeMapper;


    @RequestMapping(value = "/query1",method = RequestMethod.GET)
    public Result query1(Integer page, Integer pageSize,String noticeType,String title,String titleAndInformation) {
        //校验参数是否合法
        if (page==null||page<0){
            page=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=10;
        }
        //创建条件查询对象 先绑定排序查询
        QueryWrapper<TNotice> queryWrapper = new QueryWrapper<TNotice>().orderByDesc(NoticeConstant.weight, NoticeConstant.updateTime, NoticeConstant.createTime);
        //绑定通知类型
        if (noticeType != null&& noticeType != ""){
            queryWrapper.eq(NoticeConstant.noticeType,noticeType);
        }
        //绑定关键字查询
        if (titleAndInformation != null && titleAndInformation != ""){
            //nested嵌套查询，将方法内绑定的多个条件并存，搭配前面绑定过的其他条件 --> condition1 and ( condition2 or condition3)
            queryWrapper.nested(query->query
                    .like(NoticeConstant.title,titleAndInformation)
                    .or()
                    .like(NoticeConstant.information,titleAndInformation));
        }

        Page<TNotice> tPage = baseIService.page(new Page<>(page,pageSize),queryWrapper);

        PageView<TNotice> pageView = new PageView<TNotice>()
                .setRows(tPage.getRecords())
                .setTotalCount(tPage.getTotal())
                .setTotalPage(new Integer(tPage.getPages()+""));
        return new Result().setData(pageView).setSuccess(true);
    }


    @Override
    @Authority(auth = {"account:notice:add"})
    public Result save(@RequestBody TNotice tNotice, HttpServletRequest request) throws Exception {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //补充完善项目信息
            TUser userinfo = (TUser) request.getSession().getAttribute("USERINFO");
            if (userinfo==null){return new Result().setMsg("你还没有登录");}

            tNotice.setCreatedUser(userinfo.getUserName())
                    .setCreateTime(format.format(new Date()))
                    .setUpdatedUser(userinfo.getUserName())
                    .setUpdateTime(format.format(new Date()));
            beforeSave(tNotice);
        }catch (Exception e){
            log.error("保存失败",e);
            return new Result().setMsg("保存失败，请检查参数是否合法."+e.getMessage());
        }
        boolean save = baseIService.save(tNotice);
        return new Result().setSuccess(save);
    }

    @Override
    @Authority(auth = {"account:notice:update"})
    public Result update(@RequestBody TNotice tNotice, HttpServletRequest request) throws Exception {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            TUser userinfo = (TUser) request.getSession().getAttribute("USERINFO");
            if (userinfo==null){return new Result().setMsg("你还没有登录");}
            tNotice.setUpdateTime(format.format(new Date()))
                    .setUpdatedUser(userinfo.getUserName());
        }catch (Exception e){
            log.error("修改失败",e);
            return new Result().setMsg("修改失败，"+e.getMessage());
        }
        boolean updateById = baseIService.updateById(tNotice);
        return new Result().setSuccess(updateById);
    }

    @Override
    @Authority(auth = {"account:notice:delete"})
    public Result delete(String id) {
        return super.delete(id);
    }
}
